//
//  SCNotificationUtils.m
//  HiQ
//
//  Created by maxwell pang on 12-7-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SCNotificationUtils.h"
#import <Growl/Growl.h>
#import "PairChatWindowController.h"
#import "MainWindowController.h"

#define kGrowlNotification @"GrowlNotification"


#pragma mark - SCGrowlDelegate

//typedef struct{
//    NSUInteger type;
//    NSUInteger info;
//}CLICKCONTEXT; 

@interface SCGrowlDelegate : NSObject<GrowlApplicationBridgeDelegate>

@end

@implementation SCGrowlDelegate

- (NSDictionary *)registrationDictionaryForGrowl 
{
    NSArray *notifications = [NSArray arrayWithObject:kGrowlNotification];
    NSDictionary *registrationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:notifications, GROWL_NOTIFICATIONS_ALL, nil];
    return registrationDictionary;
}

- (void) growlNotificationWasClicked:(id)clickContext {
    // 显示添加好友提示
    if ([clickContext isEqual:@"AddNewFriend"]) {
        [[MainWindowController sharedMainWindowController] showNewFriendRequest];
        return;
    }
    if ([clickContext isEqual:@"IsAcceptAddNewFriend"]) {
        [[MainWindowController sharedMainWindowController] showIsAcceptNewFriendRequest];
        return;
    }
    
    // 显示好友聊天窗口
    NSArray *clickInfo = [clickContext componentsSeparatedByString:@"/"];
    NSString *chatType = [clickInfo objectAtIndex:0];
    NSUInteger  idInfo = [[clickInfo objectAtIndex:1] integerValue];
    if ([chatType isEqual:@"PairChat"]) {
        NSMutableArray *pairChatArray = [[MainWindowController sharedMainWindowController] pairChatArray];
        for (NSUInteger i = 0; i < [pairChatArray count]; ++i) {
            if(idInfo == [[[pairChatArray objectAtIndex:i] peerBuddy] buddyQQ])
            {
                [[MainWindowController sharedMainWindowController] showChatWindow:i];
                return;
            }
        }
    }
    if ([chatType isEqual:@"ClusterChat"]) {
        NSMutableArray *clusterChatArray = [[MainWindowController sharedMainWindowController] clusterChatArray];
        for (NSUInteger i = 0; i <[clusterChatArray count]; ++i) {
            if(idInfo == [[[[clusterChatArray objectAtIndex:i] clusterChatSource] _cluster] clusterId] )
            {
                [[MainWindowController sharedMainWindowController] showClusterChatWindow:i];
                return;
            }
        }
    }
}

@end


#pragma mark - SCNotificationUtils

@implementation SCNotificationUtils

SCGrowlDelegate *_growlDelegate;

+ (void)initialize
{
    if (_growlDelegate == nil) {
        _growlDelegate = [[SCGrowlDelegate alloc] init];
    }
    [GrowlApplicationBridge setGrowlDelegate:_growlDelegate];
}

+ (void)notifyPairChatWithTitle:(NSString *)title description:(NSString *)description peerId:(NSUInteger) peerId
{
    NSString *context = [NSString stringWithFormat:@"PairChat/%lu",peerId];
    [GrowlApplicationBridge notifyWithTitle:title
                                description:description
                           notificationName:kGrowlNotification
                                   iconData:nil
                                   priority:0
                                   isSticky:NO
                               clickContext:context];
}

+(void)notifyClusterChatWithTitle:(NSString *)title description:(NSString *)description GroupId:(NSUInteger) groupId
{
    NSString *context = [NSString stringWithFormat:@"ClusterChat/%lu",groupId];
    [GrowlApplicationBridge notifyWithTitle:title
                                description:description
                           notificationName:kGrowlNotification
                                   iconData:nil
                                   priority:0
                                   isSticky:NO
                               clickContext:context];
    
}

+ (void)notifyAddNewFriendWithTitle:(NSString *)title description:(NSString *)description
{
    [GrowlApplicationBridge notifyWithTitle:title
                                description:description
                           notificationName:kGrowlNotification
                                   iconData:nil
                                   priority:0
                                   isSticky:NO
                               clickContext:@"AddNewFriend"];
}

+ (void)notifyIsAcceptAddNewFriendWithTitle:(NSString *)title description:(NSString *)description
{
    [GrowlApplicationBridge notifyWithTitle:title
                                description:description
                           notificationName:kGrowlNotification
                                   iconData:nil
                                   priority:0
                                   isSticky:NO
                               clickContext:@"IsAcceptAddNewFriend"];
}

@end
